package base;

import java.util.ArrayList;

import algorithms.BreadthSearch;
import datastructures.*;

import comparators.BreadthSearchComparator;

public class TestCityTree {

	/**
	 * @param args
	 */
	MyNode rootNode;
	CityInfo cities;
	ArrayList<MyNode> openNodes = new ArrayList<MyNode>();
	
	private void initializeRootNode(String name){
		rootNode = new MyNode(name, cities.koords.get(name), null);
	}
	
	public TestCityTree(CityInfo cities) {
		super();
		this.cities = cities;
	}

	public ArrayList<MyNode> attachNodes(MyNode parent) {
		ArrayList<MyNode> childs = new ArrayList<MyNode>();
		
		ArrayList<String> help = cities.nachfolger.get(parent.getName());
		
		for (String city : help) {
			Koordinate k = cities.koords.get(city);
			MyNode child = new MyNode(city, k, parent);
			childs.add(child);
		}		
		parent.setChilds(childs);
		return childs;
	}
	
	public MyNode createCityGraph(CityInfo cities, String start){
		initializeRootNode(start);
		
		openNodes.add(rootNode);
		
		while (openNodes.size() > 0) {
//			openNodes.addAll(attachNodes(openNodes.get(0)));
			for (MyNode child : attachNodes(openNodes.get(0))) {
				if(!containsNode(child, openNodes)){
					openNodes.add(child);
					System.out.println("childNode added: "+child.getName());
				}
			}
			openNodes.remove(0);
		}
		
		return rootNode;
	}
	
	public boolean containsNode(MyNode node, ArrayList<MyNode> list){
		for (MyNode listNode : list) {
			if(listNode.getName().equalsIgnoreCase(node.getName())){
				return true;
			}
		}
		return false;
	}
	
	public static void main(String[] args) {

		CityInfo dLand = new CityInfo();
		TestCityTree test = new TestCityTree(dLand);
		
		test.createCityGraph(dLand, "hamburg");
		
		//breitensuche beispiel - ???
		System.out.println("======== BREITENSUCHE ========");
		BreadthSearch searchAlgo = new BreadthSearch(new BreadthSearchComparator());
		searchAlgo.searchName(test.rootNode, "muenchen");
		System.out.println("Breitensuche - untersuchte Knoten: " + searchAlgo.getSearchedNodeCount());
		
//		System.out.println("vor each");
//		for (MyNode child : dLand.kiel.getChilds()) {
//			System.out.println("kind vorhanden");
//			System.out.println(child.toString());
//		}
//		dLand.kiel.toString();
//		System.out.println("ende");
	}
}
